<?php

namespace Home\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;


class PhotoTable extends AbstractTableGateway {

    protected $table = 'photo';

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Photo());

        $this->initialize();
    }

    public function fetchAll(Select $select = null) {
        if (null === $select)
            $select = new Select();
        $select->from($this->table,array('*'))
				->join(
					'gallery',
					'photo.gallery = gallery.id',
					array('tengallery'));
        $resultSet = $this->selectWith($select);
        $resultSet->buffer();
        return $resultSet;
    }
	public function getName(Select $select = null) {
        if (null === $select)
            $select = new Select();
        $select->from($this->table,array('*'))
				->join(
					'gallery',
					'photo.gallery = gallery.id',
					array('tengallery','ngaytao','ngaycapnhat','mota'))
				->join(
					'nguoidung',
					'gallery.tacgia = nguoidung.id',
					array('hoten'));
        $resultSet = $this->selectWith($select);
        $row = $resultSet->current(); 
        return $row;
    }

    

    public function savePhoto($id,$hinhanh,$tengallery) {
		
		
        $id = (int) $id;
        if ($id == 0) {
			$data = array(
				'hinhanh' => $hinhanh,
				'tengallery' => $tengallery,
				
			);
            	
                $this->insert($data);
            
        } else {
			
			$data = array(
				'hinhanh' => $hinhanh,
				'tengallery' => $tengallery,
				
			);
            if ($this->getPhoto($id)) {
				
					$this->update($data, array('id' => $id));
				
            } else {
                throw new \Exception('Không tồn tại');
            }
        }
    }

    public function deletePhoto($id) {
        $this->delete(array('id' => $id));
    }
	public function xoaPhoto($id) {
		
        $this->delete('id IN (' . $id . ')');
    }
	

	


}
